1 00:00:00,360 --> 00:00:01,140 Hey there. 2 00:00:01,140 --> 00:00:06,150 In this lecture, we're going to be discussing and learning a feature in the Lulu programming language 3 00:00:06,150 --> 00:00:08,010 called string interpolation. 4 00:00:08,010 --> 00:00:13,410 Now the word interpolation means to insert something of a different nature into something else, which 5 00:00:13,410 --> 00:00:16,380 is exactly what string interpolation allows us to do. 6 00:00:16,380 --> 00:00:22,380 If we ever need to insert or concatenate something to a string, such as a table or the name of an instance 7 00:00:22,380 --> 00:00:27,570 or any other property of an instance, we would usually have to get that value stored in that property, 8 00:00:27,570 --> 00:00:33,210 and try to concatenate it to the table using two dots, or by using the string dot format function. 9 00:00:33,210 --> 00:00:38,340 However, there is a much easier and more efficient way of inserting these things directly into our 10 00:00:38,340 --> 00:00:41,190 strings, and that's by using string interpolation. 11 00:00:41,520 --> 00:00:46,320 So to demonstrate string interpolation, I'm going to create a new server script and server script service. 12 00:00:46,320 --> 00:00:52,290 And inside of this script what we're going to do is we're going to get the player service. 13 00:00:55,340 --> 00:00:59,600 And we're going to print a message into the console when a new player joins our game. 14 00:00:59,600 --> 00:01:06,440 So we could do players dot player added connect, get that player and then we would print out in the 15 00:01:06,440 --> 00:01:07,010 console. 16 00:01:07,010 --> 00:01:09,350 This player has joined the game. 17 00:01:09,680 --> 00:01:10,100 Now. 18 00:01:10,100 --> 00:01:15,920 Usually how we would do it is we would do player dot name and then concatenate it with this string. 19 00:01:15,920 --> 00:01:22,910 Or another way we could do it is by using the string dot format function and then placing our string 20 00:01:22,910 --> 00:01:23,930 in there. 21 00:01:23,930 --> 00:01:26,690 And then we would add a directive into the string. 22 00:01:26,690 --> 00:01:31,070 And then we would pass the string that we would like to replace that directive, which would be the 23 00:01:31,070 --> 00:01:32,000 player's name. 24 00:01:32,030 --> 00:01:37,130 See this is just like really long to type out and it doesn't look that clean. 25 00:01:37,130 --> 00:01:42,710 However, using string interpolation, we can insert the player's name directly into our string, and 26 00:01:42,710 --> 00:01:44,480 it's very easy to do so. 27 00:01:44,480 --> 00:01:50,270 To use string interpolation, we can create a new string by using the Backtick key, which is located 28 00:01:50,270 --> 00:01:53,660 directly above your tab key on the left hand side of your keyboard. 29 00:01:53,660 --> 00:01:59,360 That's right, we can actually create a string in four different ways using either double quotes, single 30 00:01:59,360 --> 00:02:04,010 quotes, double brackets, or using a Backtick. 31 00:02:04,010 --> 00:02:07,400 This backtick signifies that this string can be interpolated. 32 00:02:07,400 --> 00:02:14,600 So to insert things directly into the string, we have to use a pair of curly brackets, and between 33 00:02:14,600 --> 00:02:18,320 these brackets is where our item is going to go inside of our string. 34 00:02:18,320 --> 00:02:24,200 So after these curly brackets, I can say something like this player has joined the game, and inside 35 00:02:24,200 --> 00:02:29,360 of these curly brackets I can go ahead and insert my player dot name property. 36 00:02:29,360 --> 00:02:35,690 And just like that, we have interpolated the string by inserting this property directly into the string 37 00:02:35,690 --> 00:02:36,440 itself. 38 00:02:36,440 --> 00:02:41,450 So if I were to go and playtest my game and my player were to join, as you can see, it prints out 39 00:02:41,450 --> 00:02:42,080 my name. 40 00:02:42,080 --> 00:02:42,530 Udemy. 41 00:02:42,530 --> 00:02:49,400 Lucas has joined the game, and this looks a lot cleaner than having to use two dots to concatenate 42 00:02:49,400 --> 00:02:53,150 the string together or by using the String.format function. 43 00:02:53,150 --> 00:02:58,820 It's very easy and it looks very clean and we can insert all types of values into these interpolated 44 00:02:58,820 --> 00:02:59,240 strings. 45 00:02:59,240 --> 00:03:05,690 For example, we can print out something like I can insert this value, and then I can put a number 46 00:03:05,690 --> 00:03:10,460 in here like 1000 directly into my string. 47 00:03:10,460 --> 00:03:15,620 I don't need to use the String.format function and I don't need to use any concatenation operators. 48 00:03:15,650 --> 00:03:19,190 I'll print another string out and I'll say something like. 49 00:03:19,190 --> 00:03:25,100 I can also insert any other property or expression as well. 50 00:03:25,100 --> 00:03:29,390 And inside of here we can print out some random crap like in the workspace. 51 00:03:29,390 --> 00:03:35,150 We could print one of its properties like the global wind, and then we can insert another thing by 52 00:03:35,150 --> 00:03:36,770 putting another pair of curly brackets. 53 00:03:36,770 --> 00:03:40,280 And then I'm going to put a backslash n here to move it to a new line. 54 00:03:40,280 --> 00:03:45,080 And inside of here we could print something like, I don't know, maybe the game dot lighting. 55 00:03:45,080 --> 00:03:47,390 And we get like the time of day. 56 00:03:48,030 --> 00:03:53,070 So now if we go and playtest our game, we should have all this stuff print out in our console. 57 00:03:53,100 --> 00:03:56,910 As you can see, I can insert this value 1000 directly into my string. 58 00:03:56,910 --> 00:03:59,850 I can also insert any other property or expression as well. 59 00:03:59,850 --> 00:04:03,330 There's our global wind and then there is our time of day. 60 00:04:03,330 --> 00:04:07,320 And then once more, Udemy course has joined the game. 61 00:04:07,910 --> 00:04:12,830 So as you can see, string interpolation is very powerful and very clean looking. 62 00:04:12,830 --> 00:04:13,520 And guess what? 63 00:04:13,520 --> 00:04:14,990 That's all for me for this lecture. 64 00:04:14,990 --> 00:04:16,370 I hope you learned something new. 65 00:04:16,370 --> 00:04:19,670 And I also hope you start using this awesome feature in your code. 66 00:04:19,670 --> 00:04:21,410 See you in the next one!